/*
Please compile using  gcc -g Example.c -lpthread -std=c99 -O
 */
#include <time.h>
#include <math.h>
#include <stdlib.h> 
#include <stdio.h>
#include <pthread.h>
#define SIZE 10

pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int  array[SIZE][SIZE];
void *max(int array[SIZE]);
void *min(int array[SIZE]);
void *mean(int array[SIZE]);
void *randomfill(int array[SIZE]);
int t1 = 0, t2 = 0, t3=0;
 
int main(int argc,char *argv[])
{
int rt1,rt2,rt3;
clock_t tic = clock();
randomfill(array[SIZE]);
printf("\nIn SERIAL Code Randomly Generated array[SIZE](500 integers) in between 0 and 1000 is \n");
for(int i=0; i<SIZE; i++)
printf( "%d   ",array[SIZE][i]);
printf("\nNow, Running program's serail code:\n");
max(array[SIZE]);
mean(array[SIZE]);
min(array[SIZE]);

printf("\nNow Running program's parallel code:");
printf("\nIn PARALLEL Code Randomly Generated array[SIZE](500 integers) in between 0 and 1000 is \n");
for(int i=0; i<SIZE; i++)
printf( "%d   ",array[SIZE][i]);
  printf("\nRandomly Generated array(5 integers) in between 0 and 1000 is \n");
for(int i=0; i<SIZE; i++)
printf( "%d   ",array[i]);
pthread_t thread1,thread2,thread3;
 
  rt1=pthread_create(&thread2,
         NULL, max,
         (void *) array);
 
  rt2=pthread_create(&thread3,
         NULL, mean,
         (void *) array);
  rt3=pthread_create(&thread1,
         NULL,
         min,
         (void *) array);
//void *result;
  pthread_join(thread1, NULL);
  pthread_join(thread2, NULL);
  pthread_join(thread3, NULL);
clock_t toc = clock();
printf("\nAnd Elapsed: %f seconds \n", (double)(toc - tic) / CLOCKS_PER_SEC);
return 0;
}
 
void max(int  *array1)
{
int maxi=array1[0];
for (int i=0;i<SIZE;i++)
{
if(maxi<array1[i])
maxi=array1[i];
printf("\nNow i m running loop = %d and max = %d",i, maxi);
}
printf("\nThe maximum of the given array is %d :",maxi);
}
 
void min(int  *array2)
{
int mi=array2[0];
for (int j=0;j<SIZE;j++)
{
if(mi>array2[j])
mi=array2[j];
printf("\nNow i m running loop = %d and min = %d",j,mi);
}
printf("\nThe minimum  of the given array is %d :",mi);
}

void mean(int  *array3)
{
float mea=0.0,mea1=0.0;
for (int k=0;k<SIZE;k++)
{
mea=mea+array3[k];
mea1=mea/5;
printf("\nNow i m running mean %d and sum = %d",k, mea);
}
printf("\nThe mean  of the given array is %.3f ",mea1);
}
void randomfill(int  *array)
{
 srand((unsigned)time(0));
 for(int m=0; m<SIZE; m++)
 array[m] = (rand()%1000)+0;
}
